<?php
// +-------------------------------------------------
// | Name: 星空框架 地区管理服务
// +-------------------------------------------------
// | Version: V5.0
// +-------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-------------------------------------------------
// | Time: 2021-04-19 17:20
// +-------------------------------------------------
declare(strict_types=1);

namespace app\common\services;
use app\common\model\Area AS AreaModel;

/**
 * @xk 地区管理服务
 */
class AreaServices
{
  /**
   * @xk 执行地区更新
   *
   * @method doUpdate
   * @return [type]                  [description]
   * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
   * @Time   2021-04-19T17:22:27+080
   */
  public static function doUpdate(){
    $areaModel = new AreaModel();
    $url = 'https://restapi.amap.com/v3/config/district?key=1209f58cc5b6069bc474f1a6d09e5abf&subdistrict=6&keywords=100000';
    $data = https_request($url);
    $dataArray = json_decode($data, true);
    $arealist = $dataArray['districts'][0]['districts'];
    $timeKey = array_column($arealist, 'adcode'); //取出数组中status的一列，返回一维数组
    array_multisort($timeKey, SORT_ASC, $arealist);
    $p_code = '100000';
    if ($arealist) {
        foreach ($arealist as $key => $value) {
            $push_data['city_code'] = $value['citycode'];
            $push_data['code'] = $value['adcode'];
            $push_data['p_code'] = $p_code ? $p_code : '100000';
            $push_data['name'] = $value['name'];
            $push_data['level'] = $value['level'];
            $push_data['center'] = $value['center'];
            $push_data['cn_name'] = getPinyin($value['name']);
            $push_data['first_name'] = strtoupper(getPinyin($value['name'], 'one'));
            $push_data['create_time'] = date('Y-m-d H:i:s', time());
            $areaModel->create($push_data);
            $nextlist = $value['districts'];
            $nextkey = array_column($nextlist, 'adcode'); //取出数组中status的一列，返回一维数组
            array_multisort($nextkey, SORT_ASC, $nextlist);
            if ($nextlist) {
                foreach ($nextlist as $k => $val) {
                    $push1_data['city_code'] = $val['citycode'];
                    $push1_data['code'] = $val['adcode'];
                    $push1_data['p_code'] = $value['adcode'];
                    $push1_data['name'] = $val['name'];
                    $push1_data['level'] = $val['level'];
                    $push1_data['center'] = $val['center'];
                    $push1_data['cn_name'] = getPinyin($val['name']);
                    $push1_data['first_name'] = strtoupper(getPinyin($val['name'], 'one'));
                    $push1_data['create_time'] = date('Y-m-d H:i:s', time());
                    $areaModel->create($push1_data);
                    $nextlist1 = $val['districts'];
                    $nextkeyx = array_column($nextlist1, 'adcode'); //取出数组中status的一列，返回一维数组
                    array_multisort($nextkeyx, SORT_ASC, $nextlist1);
                    if ($nextlist1) {
                        foreach ($nextlist1 as $kx => $valx) {
                            $push2_data['city_code'] = $valx['citycode'];
                            if($valx['level'] == 'street'){
                              $push2_data['code'] = $valx['adcode'].str_pad((string)($kx+1), 4, '0', STR_PAD_LEFT);
                            }else{
                              $push2_data['code'] = $valx['adcode'];
                            }
                            $push2_data['p_code'] = $val['adcode'];
                            $push2_data['name'] = $valx['name'];
                            $push2_data['level'] = $valx['level'];
                            $push2_data['center'] = $valx['center'];
                            $push2_data['cn_name'] = getPinyin($valx['name']);
                            $push2_data['first_name'] = strtoupper(getPinyin($valx['name'], 'one'));
                            $push2_data['create_time'] = date('Y-m-d H:i:s', time());
                            $areaModel->create($push2_data);
                            $nextlist2 = $valx['districts'];
                            $nextkey1x = array_column($nextlist2, 'adcode'); //取出数组中status的一列，返回一维数组
                            array_multisort($nextkey1x, SORT_ASC, $nextlist2);
                            if ($nextlist2) {
                                foreach ($nextlist2 as $kxx => $valxx) {
                                    $push3_data['city_code'] = $valxx['citycode'];
                                    $push3_data['code'] = $valxx['adcode'].str_pad((string)($kxx+1), 4, '0', STR_PAD_LEFT);
                                    $push3_data['p_code'] = $valx['adcode'];
                                    $push3_data['name'] = $valxx['name'];
                                    $push3_data['level'] = $valxx['level'];
                                    $push3_data['center'] = $valxx['center'];
                                    $push3_data['cn_name'] = getPinyin($valxx['name']);
                                    $push3_data['first_name'] = strtoupper(getPinyin($valxx['name'], 'one'));
                                    $push3_data['create_time'] = date('Y-m-d H:i:s', time());
                                    $areaModel->create($push3_data);
                                    $nextlist3 = $valxx['districts'];
                                    $nextkey1xx = array_column($nextlist3, 'adcode'); //取出数组中status的一列，返回一维数组
                                    array_multisort($nextkey1xx, SORT_ASC, $nextlist3);
                                    if ($nextlist3) {
                                        foreach ($nextlist3 as $kxxx => $valxxx) {
                                            $push4_data['city_code'] = $valxxx['citycode'];
                                            $push4_data['code'] = $valxxx['adcode'].str_pad((string)($kxxx + 1), 4, '0', STR_PAD_LEFT);
                                            $push4_data['p_code'] = $valxx['adcode'].str_pad((string)($kxx+1), 4, '0', STR_PAD_LEFT);
                                            $push4_data['name'] = $valxxx['name'];
                                            $push4_data['level'] = $valxxx['level'];
                                            $push4_data['center'] = $valxxx['center'];
                                            $push4_data['cn_name'] = getPinyin($valxxx['name']);
                                            $push4_data['first_name'] = strtoupper(getPinyin($valxxx['name'], 'one'));
                                            $push4_data['create_time'] = date('Y-m-d H:i:s', time());
                                            // $areaModel->create($push4_data);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
  }
  public static function doUpdateX(){
    $areaModel = new AreaModel();
    $url = 'https://restapi.amap.com/v3/config/district?key=1209f58cc5b6069bc474f1a6d09e5abf&subdistrict=6&keywords=100000';
    $data = https_request($url);
    $dataArray = json_decode($data, true);
    $arealist = $dataArray['districts'][0]['districts'];
    $timeKey = array_column($arealist, 'adcode'); //取出数组中status的一列，返回一维数组
    array_multisort($timeKey, SORT_ASC, $arealist);
    if ($arealist) {
        foreach ($arealist as $key => $value) {
            $push_data['city_code'] = $value['citycode'];
            $push_data['code'] = $value['adcode'];
            $push_data['p_code'] = $p_code ? $p_code : '100000';
            $push_data['name'] = $value['name'];
            $push_data['level'] = $value['level'];
            $push_data['center'] = $value['center'];
            $push_data['cn_name'] = getPinyin($value['name']);
            $push_data['first_name'] = strtoupper(getPinyin($value['name'], 'one'));
            $push_data['create_time'] = date('Y-m-d H:i:s', time());
            $areaModel->create($push_data);
            $nextlist = $value['districts'];
            $nextkey = array_column($nextlist, 'adcode'); //取出数组中status的一列，返回一维数组
            array_multisort($nextkey, SORT_ASC, $nextlist);
            if ($nextlist) {
                foreach ($nextlist as $k => $val) {
                    $push1_data['city_code'] = $val['citycode'];
                    $push1_data['code'] = $val['adcode'];
                    $push1_data['p_code'] = $value['adcode'];
                    $push1_data['name'] = $val['name'];
                    $push1_data['level'] = $val['level'];
                    $push1_data['center'] = $val['center'];
                    $push1_data['cn_name'] = getPinyin($val['name']);
                    $push1_data['first_name'] = strtoupper(getPinyin($val['name'], 'one'));
                    $push1_data['create_time'] = date('Y-m-d H:i:s', time());
                    $areaModel->create($push1_data);
                    $nextlist1 = $val['districts'];
                    $nextkeyx = array_column($nextlist1, 'adcode'); //取出数组中status的一列，返回一维数组
                    array_multisort($nextkeyx, SORT_ASC, $nextlist1);
                    if ($nextlist1) {
                        foreach ($nextlist1 as $kx => $valx) {
                            $push2_data['city_code'] = $valx['citycode'];
                            $push2_data['code'] = $valx['adcode'];
                            $push2_data['p_code'] = $val['adcode'];
                            $push2_data['name'] = $valx['name'];
                            $push2_data['level'] = $valx['level'];
                            $push2_data['center'] = $valx['center'];
                            $push2_data['cn_name'] = getPinyin($valx['name']);
                            $push2_data['first_name'] = strtoupper(getPinyin($valx['name'], 'one'));
                            $push2_data['create_time'] = date('Y-m-d H:i:s', time());
                            $areaModel->create($push2_data);
                            $nextlist2 = $valx['districts'];
                            $nextkey1x = array_column($nextlist2, 'adcode'); //取出数组中status的一列，返回一维数组
                            array_multisort($nextkey1x, SORT_ASC, $nextlist2);
                            if ($nextlist2) {
                                foreach ($nextlist2 as $kxx => $valxx) {
                                    $push3_data['city_code'] = $valxx['citycode'];
                                    $push3_data['code'] = $valxx['adcode'].str_pad($kxx + 1, 4, '0', STR_PAD_LEFT);
                                    $push3_data['p_code'] = $valx['adcode'];
                                    $push3_data['name'] = $valxx['name'];
                                    $push3_data['level'] = $valxx['level'];
                                    $push3_data['center'] = $valxx['center'];
                                    $push3_data['cn_name'] = getPinyin($valxx['name']);
                                    $push3_data['first_name'] = strtoupper(getPinyin($valxx['name'], 'one'));
                                    $push3_data['create_time'] = date('Y-m-d H:i:s', time());
                                    $areaModel->create($push3_data);
                                    $nextlist3 = $valxx['districts'];
                                    $nextkey1xx = array_column($nextlist3, 'adcode'); //取出数组中status的一列，返回一维数组
                                    array_multisort($nextkey1xx, SORT_ASC, $nextlist3);
                                    if ($nextlist3) {
                                        foreach ($nextlist3 as $kxxx => $valxxx) {
                                            $push4_data['city_code'] = $valxxx['citycode'];
                                            $push4_data['code'] = $valxxx['adcode'].str_pad($kxxx + 1, 4, '0', STR_PAD_LEFT);
                                            $push4_data['p_code'] = $valxx['adcode'].str_pad($kxx + 1, 4, '0', STR_PAD_LEFT);
                                            $push4_data['name'] = $valxxx['name'];
                                            $push4_data['level'] = $valxxx['level'];
                                            $push4_data['center'] = $valxxx['center'];
                                            $push4_data['cn_name'] = getPinyin($valxxx['name']);
                                            $push4_data['first_name'] = strtoupper(getPinyin($valxxx['name'], 'one'));
                                            $push4_data['create_time'] = date('Y-m-d H:i:s', time());
                                            $areaModel->create($push4_data);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
  }
}
